package org.hawk.gwt.ppc.reflect;

import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

import org.hawk.gwt.ppc.javacc.ParseException;
import org.hawk.gwt.ppc.reflect.lang.ClassDelegate;
import org.hawk.gwt.ppc.reflect.lang.ClassLoaderDelegate;

import junit.framework.TestCase;

public class ReflectionProcessingUnitTestCase extends TestCase {
	public void testEntryPoint() throws IOException, ParseException{
		ReflectionProcessingUnit ppc = new ReflectionProcessingUnit();
		StringWriter writer = new StringWriter();
		ppc.processEntryPoint("org.hawk.gwt.ppc.reflect.mock.Mock1", 
				new StringReader(getMock1()), writer);
		assertEquals(getExpected(), writer.toString());
	}

	private String getMock1() throws IOException {
		FileReader reader = new FileReader("./src/org/hawk/gwt/ppc/reflect/mock/Mock1.java");
		StringWriter out = new StringWriter();
		char[] cbuf = new char[1024];
		int len = 0;
		while((len = reader.read(cbuf))>0){
			out.write(cbuf,0,len);
		}
		return out.toString();
	}
	
	private String getExpected() throws IOException {
		String mock = getMock1();
		int index = mock.lastIndexOf('}');
		return mock.substring(0,index)+
			getClassLoader()+
			mock.substring(index);
	}

	private String getClassLoader() {
		final String ClassDelegate = ClassDelegate.class.getName();
		return "static {\n" +
			ClassDelegate+".registerClassLoader(\n"+
			"new "+ClassLoaderDelegate.class.getName()+"(){\n" +
			"protected "+ClassDelegate+"<?>[] getClasses(){\n"+
			"return new "+ClassDelegate+"[]{\n" +
			getCollectedClassesAsArrayDefinition()+
			"};\n}\n"+//end of getClasses() method
			"protected String getPackage(){\n" +
			"return \""+getCommonPackage()+"\";" +
			"}\n"+//end of getPackage() method
			"protected Class<?>[] getNotReflectableClasses(){\n"+
			"return new Class[]{\n" +
			getImportedClasses()+
			"};\n}\n"+//end of getNotReflectableClasses() method
			"protected Class<?>[] getInterfaces(){\n" +
			"return new Class[]{\n" +
			"};\n" +
			"}\n" +
			"});\n" +//end of ClassLoaderDelegate inner class
			"}\n";//end of static
	}

	private String getImportedClasses() {
		return "";
	}

	private String getCommonPackage() {
		return "";
	}

	private String getCollectedClassesAsArrayDefinition() {
		return "";
	}
}
